package com.a3dweijing.wetouch2.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Activity.SpecialBriefActivity;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.ImageLoaderUtil;
import com.a3dweijing.wetouch2.adapter.SampleAdapter;
import com.a3dweijing.wetouch2.adapter.SpecialBottomRecAdapter;
import com.a3dweijing.wetouch2.bean.Special;
import com.a3dweijing.wetouch2.bean.SpecialMsg;
import com.a3dweijing.wetouch2.callback.OnClick;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.a3dweijing.wetouch2.divView.AutoLinearLayoutManager;
import com.a3dweijing.wetouch2.divView.XGallery;
import com.google.gson.Gson;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * Created by Yxd on 2017/10/31.
 */

public class ThreeDSpecialFragment extends Fragment implements OnClick {

    private Handler handler;
    private RoundedImageView roundedImageView;
    private TextView name;
    private TextView des;
    private XGallery xGallery;
    private RecyclerView bottomRec;
    private SpecialBottomRecAdapter adapter;
    private List<Special> specialList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_threed_special,container,false);
        roundedImageView= (RoundedImageView) view.findViewById(R.id.imageView);
        name= (TextView) view.findViewById(R.id.name);
        des= (TextView) view.findViewById(R.id.des);
        roundedImageView.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.timg));
        xGallery= (XGallery) view.findViewById(R.id.xgallery);
        bottomRec= (RecyclerView) view.findViewById(R.id.special_bottom_Rec);
        bottomRec.setLayoutManager(new AutoLinearLayoutManager(getContext()));
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what)
                {
                    case 100:
                        SpecialMsg specialMsg=new Gson().fromJson(msg.obj.toString(),SpecialMsg.class);
                        if (specialMsg!=null && specialMsg.getSpecialtopicList() !=null && specialMsg.getSpecialTopicList2()!=null){
                            xGallery.setAdapter(new SampleAdapter(specialList=specialMsg.getSpecialtopicList()));
                            ImageLoaderUtil.getLoader().loadImg4(specialList.get(0).getSpecial_topic_img(),roundedImageView);
                            name.setText(specialList.get(0).getSpecialtopic_headline());
                            des.setText(specialList.get(0).getSpecial_topic_describe());
                            xGallery.setSelection(100,false);
                            bottomRec.setAdapter(adapter=new SpecialBottomRecAdapter(specialMsg.getSpecialTopicList2()));
                            adapter.setOnClick(ThreeDSpecialFragment.this);
                        }
                        break;
                    case 404:
                        Toast.makeText(getContext(),"网络繁忙，请稍后再试，", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
        new GetSpecialThread("1").start();
        xGallery.setOnGalleryPageSelectListener(new XGallery.OnGalleryPageSelectListener() {
            @Override
            public void onGalleryPageSelected(int position) {
                Log.e("--", "onGalleryPageSelected: "+position);
                ImageLoaderUtil.getLoader().loadImg4(specialList.get(position).getSpecial_topic_img(),roundedImageView);
                name.setText(specialList.get(position).getSpecialtopic_headline());
                des.setText(specialList.get(position).getSpecial_topic_describe());
            }
        });
        return view;
    }

    public static ThreeDSpecialFragment newinstance()
    {
        Bundle bundle=new Bundle();
        bundle.putString("name","专题");
        ThreeDSpecialFragment threeDSpecialFragment =new ThreeDSpecialFragment();
        threeDSpecialFragment.setArguments(bundle);
        return threeDSpecialFragment;
    }

    @Override
    public void onclick(int position,Object o) {
        Special special= (Special) o;
        Intent toSpecialBrief=new Intent(getContext(), SpecialBriefActivity.class);
        toSpecialBrief.putExtra("special",special);
        startActivity(toSpecialBrief);

    }

    private class GetSpecialThread extends Thread
    {
        String pageNo;

        public GetSpecialThread(String pageNo) {
            this.pageNo = pageNo;
        }

        @Override
        public void run() {
            super.run();
            String resulet=new Getdataimpl().remotGetSpecial(pageNo);
            if (resulet!=null && resulet.length()>7)
            {
                handler.sendMessage(handler.obtainMessage(100,resulet));
            }else {
                handler.sendMessage(handler.obtainMessage(404,"服务器繁忙，请稍后再试。"));
            }
        }
    }
}
